package com.lambkit.module.cms.web.api.controller;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.lambkit.module.cms.common.CmsResult;
import com.lambkit.module.cms.core.search.SearcherKit;
import com.lambkit.module.cms.core.search.impl.FileSearchRecord;
import com.lambkit.module.cms.rpc.model.CmsArticle;
import com.lambkit.module.cms.rpc.model.CmsFile;
import com.lambkit.web.controller.LambkitController;

/**
 * 搜索api
 */
public class SearchApiController extends LambkitController {

	//private static final Log LOG = Log.getLog(SearchController.class);

	public void index() {
		String keyword = getPara("s");
		int page = getParaToInt("p", 1);
		Page<CmsArticle> articles = SearcherKit.articleSearcher().search(keyword, page, 10);
		CmsFile fileInfo = getModel(CmsFile.class, "search");
		if(fileInfo==null) {
			fileInfo = new CmsFile();
			fileInfo.setFileName(keyword);
			fileInfo.setFileCategoryId(1L);
		}
		Page<FileSearchRecord> files = SearcherKit.fileSearcher().search(fileInfo, null, null, page, 0);
		Kv data = Kv.by("article", articles).set("file", files);
		renderJson(CmsResult.success(data));
	}

	public void article() {
		String keyword = getPara("s");
		int page = getParaToInt("p", 1);
		Page<CmsArticle> articles = SearcherKit.articleSearcher().search(keyword, page, 10);
		renderJson(CmsResult.success(articles));
	}
	
	public void file() {
		String keyword = getPara("k");
		int page = getParaToInt("p", 1);
		int flag = getParaToInt("f", 0);
		CmsFile fileInfo = getModel(CmsFile.class, "search");
		if(fileInfo==null) {
			fileInfo = new CmsFile();
			fileInfo.setFileName(keyword);
			fileInfo.setFileCategoryId(1L);
		}
		Page<FileSearchRecord> files = SearcherKit.fileSearcher().search(fileInfo, keyword, null, page, flag);
		renderJson(CmsResult.success(files));
	}
}